package com.cskaoyan.mall.dao;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.admin.Admin;
import com.cskaoyan.mall.model.ChangePwd;
import com.cskaoyan.mall.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.cskaoyan.mall.util.UpdateOrAddAdminUtils.judgePassword;

/**
 * @author: jyf2000jyf
 */
public class AdminDaoImpl implements AdminDao {

    @Override
    public int login(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            Long query = queryRunner.query("select count(*) from admin where username = ? and password = ?", new ScalarHandler<>(), admin.getUsername(), admin.getPassword());
            if (query == 1) {
                return 200;
            } else {
                return 404;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 500;
    }

    @Override
    public List<Admin> getAllAdmins() {
        List<Admin> adminList = null;
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            adminList = queryRunner.query("select * from admin", new BeanListHandler<>(Admin.class));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            return adminList;
        }
    }

    @Override
    public boolean deleteAdmins(int id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("delete from admin where id = ?", id);
        } catch (SQLException e) {
            return false;
        }
        return true;
    }

    @Override
    public Admin getAdminsInfo(int id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Admin admin = null;
        try {
            admin = queryRunner.query("select * from admin where id = ?", new BeanHandler<>(Admin.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public boolean updateAdminss(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("update admin set username = ? , nickname = ? , password = ? where id = ?", admin.getUsername(), admin.getNickname(), admin.getPassword(), admin.getId());
        } catch (SQLException e) {
            return false;
        }
        return true;
    }

    @Override
    public List<Admin> getSearchAdmins(Admin admin) {
        List<Admin> adminList = null;
        // condition作为where及以后的判断条件
        String sql = "select * from admin where 1 = 1";
        List<String> paramList = new ArrayList<>();
        String usernameString = " and username like ?";
        String nicknameString = " and nickname like ?";
        // 搜索username
        if (!StringUtils.isEmpty(admin.getUsername())) {
            sql = sql + usernameString;
            paramList.add("%" + admin.getUsername() + "%");
        }
        // 搜索nickname
        if (!StringUtils.isEmpty(admin.getNickname())) {
            sql = sql + nicknameString;
            paramList.add("%" + admin.getNickname() + "%");
        }
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            adminList = queryRunner.query(sql, new BeanListHandler<>(Admin.class), paramList.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            return adminList;
        }
    }

    @Override
    public boolean addAdminss(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("insert admin user values(?,?,?,?)", null, admin.getUsername(), admin.getNickname(), admin.getPassword());
        } catch (SQLException e) {
            return false;
        }
        return true;
    }

    @Override
    public String changePwd(ChangePwd pwd) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Admin admin = new Admin(null, pwd.getAdminToken(), null, pwd.getOldPwd());
        try {
            Long query = queryRunner.query("select count(*) from admin where username = ? and password = ?", new ScalarHandler<>(), admin.getUsername(), admin.getPassword());
            if (query.intValue() == 0) {
                return "原密码错误";
            }
            if (!judgePassword(pwd.getNewPwd())) {
                return "密码正则验证未通过";
            }
            queryRunner.update("update admin set password = ? where username = ? and password = ?", pwd.getNewPwd(), admin.getUsername(), admin.getPassword());
        } catch (SQLException e) {
            return "sql异常";
        }
        return pwd.getNewPwd();
    }
}
